function loan() {
    var submitData = {
        userAge:'',//用户年龄
        userCreditCard:'',//是否有过信用卡
        liveTime:'',//在该城市居住时间
        houseLoan:'',//在该城市是否有过房贷
        userCar:'',//名下私家车
        carLoan:'',//车贷情况
        carMortgage:'',//是否接受车贷抵押
        insurancePolicy:'',//寿险保单
        insurancePremiums:'',//寿险保费
        // channel:'01'//用户渠道01表示移动端
    };
    var validataFlag = false;
    var repeatSubmit = true;
    var selCity,channelSource,source,outerSource,downappId,str;
    init();
    getUrlParam();
    function init(){
        // initWebtrends();
        initPicker();
        bindEvent();
        submitAjax();
    }

//     function initWebtrends(){
//         if (typeof(_tag) != "undefined") {
// //                    window.WTjson = window.WTjson||{};
//             _tag.dcsGetIdAsync();
//             _tag.dcsCollect();
//         }
//         if (typeof (pa_sdcajax) == "function") {
//             pa_sdcajax(
//                 'WT.ti','贷款申请页面',true,
//                 'WT.pagetitle',document.title,true,
//                 'WT.pageurl','https://'+window.location.hostname+window.location.pathname,true
//             );
//         }
//     }
    function getUrlParam(){
        var url = window.location.href,arr = [];
        channelSource = getQueryString(url,'channel_source')? getQueryString(url,'channel_source'):'';//渠道来源
        source = getQueryString(url,'source')? getQueryString(url,'source'):'';//获客渠道
        outerSource = getQueryString(url,'outerSource')? getQueryString(url,'outerSource'):'';//获客路径
        downappId = getQueryString(url,'downapp_id')? getQueryString(url,'downapp_id'):'';//获客路径

        channelSource !== ''?arr.push('channel_source=' + channelSource):null;
        source !== ''?arr.push('source=' + source):null;
        outerSource !== ''?arr.push('outerSource=' + outerSource):null;
        downappId !== ''?arr.push('downapp_id=' + downappId):null;

        str = (arr.length !== 0?'?':'') + arr.join('&');
    }
    function initPicker(){
        var city_picker1 = new mui.PopPicker({layer:2});
        city_picker1.setData(init_city_picker);
        $("#city_text1").on("tap", function(){
            setTimeout(function(){
                city_picker1.show(function(items){
                    $('[data-type="city"]').parents('.input-group').find('.input-error').hide();
                    $('.txt').addClass('sel');
                    $("#city_text1").html((items[0] || {}).text +(items[1] || {}).text);
                    submitData.cityCode = (items[1] || {}).value;
                    selCity = (items[0] || {}).text +'-' +(items[1] || {}).text;
                    //console.log(selCity);
                });
            },200);
        });
    }
    function bindEvent(){
        $('.input-radio').on('click','li',function () {
            $(this).addClass("active");
            $(this).siblings('li').removeClass("active");
            var dataType = $(this).parent('ul').attr('data-type');
            var selectValue = $(this).children('label').attr('data-val');
            //console.log($(this).parent('ul').attr('data-type'))
            //console.log($(this).children('label').text());
            switch (dataType){
                //年龄
                case 'userAge':
                    submitData.userAge = selectValue;
                    $('[data-type="userAge"]').parents('.input-group').find('.input-error').hide();
                    break;
                //是否有过信用卡
                case 'userCreditCard':
                    submitData.userCreditCard = selectValue;
                    $('[data-type="userCreditCard"]').parents('.input-group').find('.input-error').hide()
                    break;
                //城市居住时间
                case 'liveTime':
                    submitData.liveTime = selectValue;
                    $('[data-type="liveTime"]').parents('.input-group').find('.input-error').hide()
                    break;
                //在该城市是否有过房贷
                case 'houseLoan':
                    submitData.houseLoan = selectValue;
                    $('[data-type="houseLoan"]').parents('.input-group').find('.input-error').hide()
                    break;
                //名下是否有私家车
                case 'userCar':
                    submitData.userCar = selectValue;
                    $('[data-type="userCar"]').parents('.input-group').find('.input-error').hide()
                    break;
                //车贷剩余还款时间
                case 'carLoan':
                    submitData.carLoan = selectValue;
                    $('[data-type="carLoan"]').parents('.input-group').find('.input-error').hide()
                    break;
                //是否接受车辆抵押
                case 'carMortgage':
                    submitData.carMortgage = selectValue;
                    $('[data-type="carMortgage"]').parents('.input-group').find('.input-error').hide()
                    break;
                //是否购买过寿险保单
                case 'insurancePolicy':
                    submitData.insurancePolicy = selectValue;
                    $('[data-type="insurancePolicy"]').parents('.input-group').find('.input-error').hide()
                    break;
                //寿险年保费合计
                case 'insurancePremiums':
                    submitData.insurancePremiums = selectValue;
                    $('[data-type="insurancePremiums"]').parents('.input-group').find('.input-error').hide()
                    break;
            };
            if(submitData.userCar== 'YES'){
                $('.loantrust-car').show();
                $('.car-mortgage').show();
            }else{
                $('.loantrust-car').hide();
                $('.car-mortgage').hide();
                submitData.carLoan = '';
                submitData.carMortgage = '';
                $('.loantrust-car').find('li').removeClass("active");
                $('.car-mortgage').find('li').removeClass("active");

            }
            if(submitData.insurancePolicy == 'YES'){
                $('.insurance-premiums').show();
            }else{
                $('.insurance-premiums').hide();
                submitData.insurancePremiums = '';
                $('.insurance-premiums').find('li').removeClass("active");
            }
        });
        $('[data-type="userName"]').on('blur',function () {
            var userName = $('[data-type="userName"]').val().trim();
            if(/[\u4e00-\u9fa5]{2,}$/gi.test(userName)){
                $(this).parents('.input-group').find('.input-error').hide();
            }else{
                $(this).parents('.input-group').find('.input-error').show();
            }
        })
        $('[data-type="userPhone"]').on('blur',function () {
            var userPhone = $('[data-type="userPhone"]').val();
            if(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[5,7]{1})|(17[0,1,3,6,7,8]))+\d{8})$/.test(userPhone)){
                $(this).parents('.input-group').find('.input-error').hide();
            }else{
                $(this).parents('.input-group').find('.input-error').show();
            }
        })
        $('.new-loan').on('click',function(){
            location.href='newloan.html' + str;
        })
        $('.car-loan').on('click',function(){
            location.href='carloan.html' + str;
        })

    }
    function showError(){
        var userName = $('[data-type="userName"]').val().trim();
        var userPhone = $('[data-type="userPhone"]').val();
        var city = $('[data-type="city"]').text();
        $.each(submitData,function (key,val) {
            if(key == 'userAge' || key == 'userCreditCard'  || key == 'liveTime' || key == 'houseLoan' || key == 'userCar' || key == 'insurancePolicy'){
                if(submitData[key] == ''){
                    $('[data-type='+key+']').parents('.input-group').find('.input-error').show()
                }else{
                    $('[data-type='+key+']').parents('.input-group').find('.input-error').hide();
                    if(submitData.userCar == 'YES'){
                        if(submitData.carLoan == ''){
                            $('[data-type="carLoan"]').parents('.input-group').find('.input-error').show();
                        }
                        if(submitData.carMortgage ==''){
                            $('[data-type="carMortgage"]').parents('.input-group').find('.input-error').show();
                        }
                    }
                    if(submitData.insurancePolicy == 'YES'){
                        if(submitData.insurancePremiums == '') {
                            $('[data-type="insurancePremiums"]').parents('.input-group').find('.input-error').show();
                        }
                    }
                }
            }
        });
        if(city != '请选择城市'){
            submitData.city = selCity;
            $('[data-type="city"]').parents('.input-group').find('.input-error').hide();
        }else{
            $('[data-type="city"]').parents('.input-group').find('.input-error').show();
        }
        if(!/[\u4e00-\u9fa5]{2,}$/gi.test(userName)){
            $('[data-type="userName"]').parent().siblings().show();
        }else{
            $('[data-type="userName"]').parent().siblings().hide();
        }
        //电话号码验证
        if(!/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[5,7]{1})|(17[0,1,3,6,7,8]))+\d{8})$/.test(userPhone)){
            $('[data-type="userPhone"]').parent().siblings().show();
        }else{
            $('[data-type="userPhone"]').parent().siblings().hide();
        }
    }
    function validataSubmit() {
        var userName = $('[data-type="userName"]').val().trim();
        var userPhone = $('[data-type="userPhone"]').val();
        var city = $('[data-type="city"]').text();
        if(!/[\u4e00-\u9fa5]{2,}$/gi.test(userName)){
            validataFlag=false;
            return;
        }
        //电话号码验证
        if(!/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(14[5,7]{1})|(17[0,1,3,6,7,8]))+\d{8})$/.test(userPhone)){
            validataFlag=false;
            return;
        }
        if(!submitData.userAge || !submitData.userCreditCard || !submitData.liveTime || !submitData.houseLoan || !submitData.userCar || !submitData.insurancePolicy){
            validataFlag = false;
            return;
        }
        if(submitData.userCar && submitData.userCar == 'YES'){
            if(!submitData.carLoan || !submitData.carMortgage){
                validataFlag = false;
                return;
            }
        }
        if(submitData.insurancePolicy && submitData.insurancePolicy == 'YES'){
            if(!submitData.insurancePremiums ){
                validataFlag = false;
                return;
            }
        }
        if(!submitData.city){
            validataFlag = false;
            return;
        }
        validataFlag = true;
    }
    function submitAjax() {
        $('.btn-submit').on('click',function () {
            showError();
            validataSubmit();
            if(validataFlag && repeatSubmit){
                submitData.channelSource = channelSource;
                submitData.source = source;
                submitData.outerSource = outerSource;
                //getUrlParam();
                submitData.userName = $('[data-type="userName"]').val().trim();
                submitData.userPhone = $('[data-type="userPhone"]').val();
                repeatSubmit = false;
                //console.log(submitData);
                $.ajax({
                    url: PAB.domain.rsbDomain + '/brop/ma/cust/app/market/loan/applyCarLoan.do',
                    //url:'https://rsb.pingan.com.cn/brop/ma/cust/app/market/loan/applyCarLoan.do',//生产
                    type: 'POST',
                    data:submitData,
                    dataType:'json',
                    success:function(res){
                        repeatSubmit = true;
                        if(res.responseCode=='000000'){
                            resetStatus();
                            location.href='loansucess.html' + str;
                        }else if(res.responseCode == '900114'||res.responseCode == '900115' ){
                            window.location.href = APP.config.errorUrl;
                        }else{
                            $(".toast-area").show();
                            setTimeout('$(".toast-area").hide()',2000);
                        }
                    },
                    error:function(res){
                        $(".toast-area").show();
                        setTimeout('$(".toast-area").hide()',2000);
                        repeatSubmit = true;
                        // console.log('失败')
                    }
                })
            }
        })
    }

    function getQueryString(url, name){
        var reg = new RegExp("(^|&|\\?)" + name + "=([^(&|\\?)]*)(&|\\?|$)", "i");
        var l = decodeURI(url);
        var r = l.substr(1).match(reg);
        if (r != null){
            return unescape(r[2]);
        }
        return null;
    }
    function resetStatus(){
        $('.input-radio').children().removeClass('active');
        $('.txt').removeClass('sel');
        $('[data-type="userName"]').val('');
        $('[data-type="userPhone"]').val('');
        $('[data-type="city"]').text('请选择城市');
        selCity='';
        submitData = {
            userAge:'',//用户年龄
            userCreditCard:'',//是否有过信用卡
            liveTime:'',//在该城市居住时间
            houseLoan:'',//在该城市是否有过房贷
            userCar:'',//名下私家车
            carLoan:'',//车贷情况
            carMortgage:'',//是否接受车贷抵押
            insurancePolicy:'',//寿险保单
            insurancePremiums:'',//寿险保费
            // channel:'01'//用户渠道01表示移动端
        };
        $('.loantrust-car').hide();
        $('.car-mortgage').hide();
        $('.insurance-premiums').hide();
    }
}